#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pcap/pcap.h>
int main(int argc, char **argv)
{
	int fd;
	struct pcap_file_header pcapfh;	
	struct pcap_pkthdr *pkthdr;
	char *pktdata;
	printf("filename is:%s\n", argv[1]);
	fd = open(argv[1], O_CREAT);
	if (fd == -1) {
		printf("open failed!error code:%d\n", errno);
		return -1;
	}
	memset((char*)&pcapfh, 0, sizeof(struct pcap_file_header));
	int ret = 0;
	ret = read(fd, (char*)&pcapfh, sizeof(struct pcap_file_header));
	if (ret != sizeof(struct pcap_file_header)) {
		printf("read file header failed!\n");
		return -1;
	}	
	pkthdr = malloc(sizeof(struct pcap_pkthdr));
	pktdata = malloc(1600);

	ret = read(fd, (char*)pkthdr, sizeof(struct pcap_pkthdr));
	if (ret != sizeof(struct pcap_pkthdr)) {
		return -1;
	}
	printf("caplen:%d\nlen:%ul\ntime:%ul\n", pkthdr->caplen, pkthdr->len, 
		pkthdr->ts.tv_sec);
	ret = read(fd, pktdata, pkthdr->len);
	if (ret != pkthdr->len) {
		return -1;
	}	
	printf("data:\n%s\n", pktdata);
	
	close(fd);
	return 0;
}
